package servlet;

import com.alibaba.fastjson.JSON;
import pojo.Contact;
import pojo.User;
import service.ContactService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * 查询用户的所有联系人
 * 输入一个用户对象user
 * 输出该用户关联的所有联系人List<person>
 */
@WebServlet("/contactsOfUserServlet")
public class ContactsOfUserServlet extends HttpServlet {
    private final ContactService contactService = new ContactService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader bufferedReader = request.getReader();
        String jsonString = bufferedReader.readLine();
        User user = JSON.parseObject(jsonString, User.class);

        List<Contact> contacts = contactService.selectContactsOfUserNo(user.get_userNo());

        jsonString = JSON.toJSONString(contacts);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
